import { set_local_cache } from '@/config/cache'
import { fetch } from '@/config/fetch'
export default {
    namespaced: true,
    state: {
        isLogin: '',    // 是否登录，空为未登录
        isEditor: ''      // 是否编辑
    },
    getters: {
        isLogin: state => {
            return state.isLogin
        },
        isEditor: state => {
            return state.isEditor
        }
    },
    mutations: {
        set_isLogin (state, val) {
            state.isLogin = val
            set_local_cache('isLogin', val)
        },
        set_isEditor(state, val) {
            state.isEditor = val
        }
    },
    actions: {
        // 获取用户权限
        get_userPower_data({ commit }, userid) {
            let params = {
                type: 'check',
                userid: userid
            }
            fetch('POST', 'power', params).then(res => {
                if (res.data) {
                    console.log('editor:', res.data)
                    commit('set_isEditor', res.data)
                }
            })
        },
        // 提交注册验证码数据
        get_smsRegister_data({ state, rootState }, { mobile }) {
            let params = {
                'mobile': mobile
            }
            let res = fetch('get', 'smsRegister', params)
            return res
        },
        // 提交注册数据
        get_Register_data({ state, rootState }, { password, verify_code, truename, identity, mobile, userCode }) {
            let params = {
                'password': password,
                'verify_code': verify_code,
                'truename': truename,
                'identity': identity,
                'mobile': mobile,
                'userCode': userCode
            }
            let res = fetch('post', 'register', params)
            return res
        },
        // 提交密码登录数据
        get_passwordLogin_data({ state, rootState }, { password, mobile }) {
            let params = {
                'password': password,
                'mobile': mobile
            }
            let res = fetch('post', 'passwodLogin', params)
            return res
        },
        // 提交验证码登录数据
        get_smsLogin_data({ state, rootState }, { mobile }) {
            let params = {
                'mobile': mobile
            }
            let res = fetch('get', 'smsLogin', params)
            return res
        },
        // 提交登录数据
        get_codeLogin_data({ state, rootState }, { mobile, verify_code }) {
            let params = {
                'mobile': mobile,
                'verify_code': verify_code
            }
            let res = fetch('post', 'codeLogin', params)
            return res
        },
        // 提交验证码重置数据
        get_smsReset_data({ state, rootState }, { mobile }) {
            let params = {
                'mobile': mobile
            }
            let res = fetch('get', 'smsReset', params)
            return res
        },
        // 提交重置数据
        get_Reset_data({ state, rootState }, { mobile, verify_code, password }) {
            let params = {
                'mobile': mobile,
                'verify_code': verify_code,
                'password': password,
                'password_repeat': password
            }
            let res = fetch('post', 'reset', params)
            return res
        }
    }
}
